import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class CustomRuleSettingPage extends StatefulWidget {
  const CustomRuleSettingPage({super.key});

  @override
  State<CustomRuleSettingPage> createState() => _CustomRuleSettingPageState();
}

class _CustomRuleSettingPageState extends State<CustomRuleSettingPage> {
  var ip = "";
  var port = "";
  var station = "";

  // 初始化本地设置
  Future<void> initLocal() async {
    final prefs = await SharedPreferences.getInstance();
    var ip1 = prefs.getString("ip") ?? "";
    var port1 = prefs.getString("port") ?? "";
    var station1 = prefs.getString("station") ?? "";
    ip = ip1;
    port = port1;
    station = station1;
    setState(() {});
  }

  // 保存设置
  void saveLocal() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setString("ip", ip);
    prefs.setString("port", port);
    prefs.setString("station", station);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initLocal();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: EdgeInsets.only(left: 10, right: 10),
        child: Column(
          children: [
            SizedBox(height: 10),
            Row(
              children: [
                Expanded(
                  child: TextField(
                    onChanged: (value) {
                      ip = value;
                    },
                    controller: TextEditingController(text: ip),
                    decoration: InputDecoration(
                      labelText: "IP 地址",
                      border: OutlineInputBorder(),
                      contentPadding: const EdgeInsets.symmetric(
                        horizontal: 20,
                        vertical: 25, // 关键：垂直内边距归零
                      ),
                      isDense: true, // 启用紧凑模式
                      labelStyle: TextStyle(
                        fontSize: 20, // 标签字体调小
                      ),
                    ),
                  ),
                ),
                SizedBox(width: 20),
                Expanded(
                  child: TextField(
                    onChanged: (value) {
                      port = value;
                    },
                    controller: TextEditingController(text: port),
                    decoration: InputDecoration(
                      labelText: "Port 端口",
                      border: OutlineInputBorder(),
                      contentPadding: const EdgeInsets.symmetric(
                        horizontal: 20,
                        vertical: 25, // 关键：垂直内边距归零
                      ),
                      isDense: true, // 启用紧凑模式
                      labelStyle: TextStyle(
                        fontSize: 20, // 标签字体调小
                      ),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: 10),
            Row(
              children: [
                Expanded(
                  child: TextField(
                    onChanged: (value) {
                      station = value;
                    },
                    controller: TextEditingController(text: station),
                    decoration: InputDecoration(
                      labelText: "Station 工位",
                      border: OutlineInputBorder(),
                      contentPadding: const EdgeInsets.symmetric(
                        horizontal: 20,
                        vertical: 25, // 关键：垂直内边距归零
                      ),
                      isDense: true, // 启用紧凑模式
                      labelStyle: TextStyle(
                        fontSize: 20, // 标签字体调小
                      ),
                    ),
                  ),
                ),
                SizedBox(width: 20),
                Expanded(child: Text("")),
              ],
            ),
            SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Align(
                  alignment: Alignment.bottomLeft,
                  child: TDButton(
                    text: "Save 保存",
                    height: 60,
                    theme: TDButtonTheme.primary,
                    onTap: () {
                      saveLocal();
                      TDToast.showSuccess("保存成功!", context: context);
                    },
                  ),
                ),
                SizedBox(width: 70),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
